<?php
/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2017/8/30
 * Time: 16:56
 */

namespace app\common\controller;
use app\common\model\Userwechat;
use app\common\model\User;
use think\Controller;
use think\Session;
use fast\third\Wechat;
/**
 * Class WebFront
 * @package app\common\controller
 * 本类为微信前端基类，前端所有需要用户认证的页面全部必须基于此类
 */
class WebFront extends Controller
{
	public  $user=null;
	/**
	 * 快速搜索时执行查找的字段
	 */
	protected $searchFields = 'id';
	
	/**
	 * 是否是关联查询
	 */
	protected $relationSearch = false;
	
	public function _initialize()
    {
	  
    }
    public function returnerror(){
	    echo "ERROR!";
	    echo "ERROR!";
	    return;
    }

    /**
     * 实现用户自动登录
     */
    public function autologin(){
	   
    }
    /**
     * @return int
     * 判断是否为微信浏览器
     */
    public static function isweixin(){
        $user_agent = $_SERVER['HTTP_USER_AGENT'];
        if (strpos($user_agent, 'MicroMessenger') === false) {
            // 非微信浏览器禁止浏览
            return 0;
        } else {
            // 微信浏览器，允许访问
           return 1;
        }
    }
	/**
	 * 生成查询所需要的条件,排序方式
	 * @param mixed $searchfields 查询条件
	 * @param boolean $relationSearch 是否关联查询
	 * @return array
	 */
	public function buildparams($searchfields = null, $relationSearch = null)
	{
		$searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
		$relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
		$search = $this->request->get("search", '');
		$filter = $this->request->get("filter", '');
		$op = $this->request->get("op", '');
		$sort = $this->request->get("sort", "id");
		$order = $this->request->get("order", "DESC");
		$offset = $this->request->get("offset", 0);
		$limit = $this->request->get("limit", 0);
		$filter = json_decode($filter, TRUE);
		$op = json_decode($op, TRUE);
		$filter = $filter ? $filter : [];
		$where = [];
		$modelName = '';
		if ($relationSearch)
		{
			if (!empty($this->model))
			{
				$class = get_class($this->model);
				$name = basename(str_replace('\\', '/', $class));
				$name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $name));
				$modelName = $name . ".";
			}
			if (stripos($sort, ".") === false)
			{
				$sort = $modelName . $sort;
			}
		}
		if ($search)
		{
			$searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields);
			$searchlist = [];
			foreach ($searcharr as $k => $v)
			{
				$searchlist[] = (stripos($v, ".") !== false ? $v : "{$modelName}`{$v}`") . " LIKE '%{$search}%'";
			}
			$where[] = "(" . implode(' OR ', $searchlist) . ")";
		}
		foreach ($filter as $k => $v)
		{
			$sym = isset($op[$k]) ? $op[$k] : '=';
			if (stripos($k, ".") === false)
			{
				$k = $modelName . $k;
			}
			$sym = isset($op[$k]) ? $op[$k] : $sym;
			switch ($sym)
			{
				case '=':
				case '!=':
				case 'LIKE':
				case 'NOT LIKE':
					$where[] = [$k, $sym, $v];
					break;
				case '>':
				case '>=':
				case '<':
				case '<=':
					$where[] = [$k, $sym, intval($v)];
					break;
				case 'IN(...)':
				case 'NOT IN(...)':
					$where[] = [$k, str_replace('(...)', '', $sym), explode(',', $v)];
					break;
				case 'BETWEEN':
				case 'NOT BETWEEN':
					$where[] = [$k, $sym, array_slice(explode(',', $v), 0, 2)];
					break;
				case 'LIKE %...%':
					$where[] = [$k, 'LIKE', "%{$v}%"];
					break;
				case 'IS NULL':
				case 'IS NOT NULL':
					$where[] = [$k, strtolower(str_replace('IS ', '', $sym))];
					break;
				default:
					break;
			}
		}
		$where = function($query) use ($where) {
			foreach ($where as $k => $v)
			{
				if (is_array($v))
				{
					call_user_func_array([$query, 'where'], $v);
				}
				else
				{
					$query->where($v);
				}
			}
		};
		return [$where, $sort, $order, $offset, $limit];
	}
}